package com.dream.cgomall.service.impl;

import com.dream.cgomall.dao.CgoDiscoverDao;
import com.dream.cgomall.entity.CgoDiscover;
import com.dream.cgomall.service.ContentService;
import com.google.gson.Gson;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * ES实现类
 */
@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    RestHighLevelClient restHighLevelClient;
    @Autowired
    CgoDiscoverDao cgoDiscoverDao;

    public static final String INDEX_NAME = "cgodiscover";

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    SearchRequest searchRequest = new SearchRequest(INDEX_NAME);


    //将数据从数据库中转移到ES中
    public boolean parseContent() throws IOException {
        List<CgoDiscover> cgoDiscovers = cgoDiscoverDao.queryAll(null);
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("1m");

        Gson gson = new Gson();
        for (CgoDiscover c : cgoDiscovers) {
            String source = gson.toJson(c);
            bulkRequest.add(new IndexRequest("cgodiscover")
                    .source(source, XContentType.JSON)
            );
        }

        BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        return !bulkResponse.hasFailures();

    }

    //将新增的数据添加到数据库中

    //搜索
    @Override
    public List<Map<String, Object>> searchPage(String keyword, int pageNo, int pageSize) throws IOException {
        sourceBuilder.from(pageNo);
        sourceBuilder.size(pageSize);

        QueryBuilder match;
        if (keyword == null || "".equals(keyword)) {
            match = QueryBuilders.matchAllQuery();
        } else {
            match = QueryBuilders.matchQuery("title", keyword);
        }
        sourceBuilder.query(match);

        searchRequest.source(sourceBuilder);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        ArrayList<Map<String, Object>> list = new ArrayList<>();
        for (SearchHit documentFields : search.getHits().getHits()) {
            list.add(documentFields.getSourceAsMap());
        }
        return list;
    }

    @Override
    //3、获取这些数据实现高亮功能
    public List<Map<String, Object>> searchPageHighlightBuilder(String keyword, int pageNo, int pageSize) throws IOException {
        if (pageNo <= 1) {
            pageNo = 1;
        }
        sourceBuilder.from(pageNo);
        sourceBuilder.size(pageSize);
        QueryBuilder title
                = QueryBuilders.matchQuery("title", keyword);
        sourceBuilder.query(title);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        HighlightBuilder highlightBuilder = new HighlightBuilder();
        //HighlightBuilder用于ES中高亮条件的构建
        highlightBuilder.field("title"); //让查询中title字段中的内容高亮
        highlightBuilder.requireFieldMatch(false); //多个字段高亮显示
        highlightBuilder.preTags("<span style='color:red'>");
        highlightBuilder.postTags("</span>");
        sourceBuilder.highlighter(highlightBuilder);

        //执行搜索
        searchRequest.source(sourceBuilder);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        //解析结果
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            //解析高亮的字段
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            //highlightFields这个映射里放的是所有高亮的字段
            HighlightField title1 = highlightFields.get("title");
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.err.println("sourceAsMap：" + sourceAsMap);
            //解析高亮的字段
            if (title1 != null) {
                Text[] fragments = title1.fragments();
                //fragments是把title1所标记的高亮字段自动加上前后缀
                System.err.println("fragments:" + Arrays.toString(fragments));
                String n_title = "";
                for (Text text : fragments) {
                    n_title += text;
                }
                sourceAsMap.put("title", n_title);
            }
            list.add(sourceAsMap);
        }
        return list;
    }


}
